package com.dp.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.List;

public class ByteUtil {
	 public static BigDecimal hexFloat2BigDecimal(String hex) {
	        float value = Float.intBitsToFloat((int)Long.parseLong(hex, 16));
	        BigDecimal bd = new BigDecimal(Float.toString(value));
	        return bd.setScale(2, BigDecimal.ROUND_DOWN);
	  }
	
	/**
     * byte[] 转为16进制String
     */
    public static String bytes2HexString(byte[] b) {
        String ret = ""; 
        for (int i = 0; i < b.length; i++) { 
            String hex = Integer.toHexString(b[i] & 0xFF); 
            if (hex.length() == 1) { 
                hex = '0' + hex; 
            } 
            ret += hex.toUpperCase(); 
        } 
        return ret; 
    } 
    
    public static String convertStringToHex(String str) {
		char[] chars = str.toCharArray();
		StringBuffer hex = new StringBuffer();
		for (int i = 0; i < chars.length; i++) {
			hex.append(Integer.toHexString((int) chars[i]));
		}
		return hex.toString();
	}

	public static String convertHexToString(String hex) {
		StringBuilder sb = new StringBuilder();
		StringBuilder temp = new StringBuilder();
		// 49204c6f7665204a617661 split into two characters 49, 20, 4c...
		for (int i = 0; i < hex.length() - 1; i += 2) {
			// grab the hex in pairs
			String output = hex.substring(i, (i + 2));
			// convert hex to decimal
			int decimal = Integer.parseInt(output, 16);
			// convert the decimal to character
			sb.append((char) decimal);
			temp.append(decimal);
		}
		System.out.println("Decimal : " + temp.toString());
		return sb.toString();
	}
    
    /**
     * 从一个byte[]数组中截取一部分
     * @param src
     * @param begin
     * @param count
     * @return
     */
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        for (int i=begin;i<begin+count; i++) bs[i-begin] = src[i];
        return bs;
    }
    
    //     转化十六进制编码为字符串
    public static String toStringHex(String s)
    {
        byte[] baKeyword = new byte[s.length()/2];
        for(int i = 0; i < baKeyword.length; i++)
        {
          try
          {
              baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
        }
     
        try 
        {
            s = new String(baKeyword, "utf-8");//UTF-16le:Not
        } 
        catch (Exception e1) 
        {
            e1.printStackTrace();
        } 
        return s;
    }
    
    public static int byteArrayToInt(byte[] b) {   
    	return   b[3] & 0xFF |   
    	            (b[2] & 0xFF) << 8 |   
    	            (b[1] & 0xFF) << 16 |   
    	            (b[0] & 0xFF) << 24;   
    	}   
    	public static byte[] intToByteArray(int a) {   
    	return new byte[] {   
    	        (byte) ((a >> 24) & 0xFF),   
    	        (byte) ((a >> 16) & 0xFF),      
    	        (byte) ((a >> 8) & 0xFF),      
    	        (byte) (a & 0xFF)   
    	    };   
    	} 
    	public static Integer hexToInt(String str){
 		   String t = str;

 		   char[] ch = t.toCharArray();
 		   String res = "";
 		   for(int i = 0; i < str.length(); i += 2){
 		      res += Integer.parseInt(ch[i]+ch[i+1]+"", 16);
 		   }

 		   return Integer.parseInt(res);
 		}
    	
    	public static Integer hexToInt(String str, int start, int end){
    		   String t = str.substring(start, end);

    		   char[] ch = t.toCharArray();
    		   String res = "";
    		   for(int i = 0; i < end-start; i += 2){
    		      res += Integer.parseInt(ch[i]+ch[i+1]+"", 16);
    		   }

    		   return Integer.parseInt(res);
    		}
    	
    	public static String bytesToAscii(byte[] bytes, int offset, int dateLen) {  
            if ((bytes == null) || (bytes.length == 0) || (offset < 0) || (dateLen <= 0)) {  
                return null;  
            }  
            if ((offset >= bytes.length) || (bytes.length - offset < dateLen)) {  
                return null;  
            }  
      
            String asciiStr = null;  
            byte[] data = new byte[dateLen];  
            System.arraycopy(bytes, offset, data, 0, dateLen);  
            try {  
                asciiStr = new String(data, "ISO8859-1");  
            } catch (UnsupportedEncodingException e) {  
            }  
            return asciiStr;  
        }  
    	
    	//16进制字符串转化为二进制数据
    	public static byte[] hexStringToByte(String hex) {  
            int len = (hex.length() / 2);  
            byte[] result = new byte[len];  
            char[] achar = hex.toCharArray();  
            for (int i = 0; i < len; i++) {  
                int pos = i * 2;  
                result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));  
            }  
            return result;  
        } 
    	
    	public static byte[] hexToBytes(String hex) {
 	        int len = hex.length();
 	        byte[] data = new byte[len / 2];
 	        for (int i = 0; i < len; i += 2) {
 	            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
 	                                 + Character.digit(hex.charAt(i+1), 16));
 	        }
 	        return data;
 	    }
      
        private static byte toByte(char c) {  
            byte b = (byte) "0123456789abcdef".indexOf(c);  
            return b;  
        }  
        
        public static int getXor(byte[] datas){

        	 int result=0;
        	  for (int i = 0; i <datas.length; i++) { 
        	    result= result^ (datas[i] & 0xFF);
        	    //System.out.println(result+";"+(datas[i] & 0xFF));
        	  }
            return result;
        }

    public static void main(String[] args) {
        String ss = "FFEE21001E52000761000000005200076102013030303030303637343731353030303331342D";
        byte[] bytes = hexToBytes(ss);
        System.out.println(bytes2HexString(bytes));
        System.out.println(new String());
    }
}
